<?php
require_once 'config.php'; 
require_once 'Tools.class.php'; 
require_once 'HTML.class.php'; 
class WeiChat{

    //构造发送消息=文本类型
    public static function SendText($fromUsername,$toUsername,$contentStr){
        //$contentStr=str_replace("<", "&lt;", $contentStr);
        //$contentStr=str_replace(">", "&gt;", $contentStr);
        //$contentStr=str_replace("\"", "&quot;", $contentStr);
        Tools::wxLog($fromUsername,$contentStr,"receive");//消息日志记录
        $contentStr=str_replace("_OPID", $fromUsername, $contentStr);
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    <FuncFlag>0</FuncFlag>
                    </xml>";             
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), "text", $contentStr);
        echo $resultStr;
    }
    
    //发送图片信息
    public static function SendPic($fromUsername,$toUsername,$title,$desc,$pic,$url){
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <ArticleCount>1</ArticleCount>
                    <Articles>
                    <item>
                    <Title><![CDATA[%s]]></Title> 
                    <Description><![CDATA[%s]]></Description>
                    <PicUrl><![CDATA[%s]]></PicUrl>
                    <Url><![CDATA[%s]]></Url>
                    </item>
                    </Articles>
                    </xml> ";   
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), "news",$title,$desc,$pic,$url);
        echo $resultStr;
    }
	
	//检验来源真实性
    public static function checkSignature(){
        $token = TOKEN;
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
	
	//xml对象解析
	public static function getWeiChatXML($Str){
		libxml_disable_entity_loader(true);
        $postObj = simplexml_load_string($Str, 'SimpleXMLElement', LIBXML_NOCDATA);
		return $postObj;
	}

     public static function SendMenu($fromUsername,$toUsername){
        $textTpl = "<xml>
                    <ToUserName><![CDATA[".$fromUsername."]]></ToUserName>
                    <FromUserName><![CDATA[".$toUsername."]]></FromUserName>
                    <CreateTime>".time()."</CreateTime>
                    <MsgType><![CDATA[news]]></MsgType>
                    <ArticleCount>3</ArticleCount>
                    <Articles>
                        <item>
                        <Title><![CDATA[智能德院微信公众平台]]></Title> 
                        <Description><![CDATA[1]]></Description>
                        <PicUrl><![CDATA[".API_URL."weixin/web/images/logo.png]]></PicUrl>
                        <Url><![CDATA[http://www.baidu.com]]></Url>
                        </item>
                        <item>
                        <Title><![CDATA[课表查询]]></Title> 
                        <Description><![CDATA[1.当天课程 2.明天课程 3.本周课程]]></Description>
                        <PicUrl><![CDATA[".API_URL."weixin/web/images/table.png]]></PicUrl>
                        <Url><![CDATA[http://www.baidu.com]]></Url>
                        </item>
                        <Title><![CDATA[课表查询]]></Title> 
                        <Description><![CDATA[1.当天课程 2.明天课程 3.本周课程]]></Description>
                        <PicUrl><![CDATA[".API_URL."weixin/web/images/table.png]]></PicUrl>
                        <Url><![CDATA[http://www.baidu.com]]></Url>
                        </item>
                    </Articles>
                    </xml> "; 
        sae_debug($textTpl); 
        echo $textTpl;
    }
}
?>